package com.sun.tv;

import java.io.IOException;
import java.lang.ref.WeakReference;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;

public abstract class Click
{
	
	static final String		CONFIG_NAME_TYPE	= "type";
	static final String		CONFIG_NAME_TARGET	= "target";
	static final String     CONFIG_NAME_BG      = "bg";
	static final String     CONFIG_NAME_JUMP    = "jump";
	
	static final String		CONFIG_NAME_IOS		= "1";
	static final String		CONFIG_NAME_DEFAULT	= "0";
	static final String		CONFIG_NAME_ANDROID	= "2";
	static final String		CONFIG_NAME_S60		= "3";
	static final String		CONFIG_NAME_MTK		= "4";
	
	static final String		CONFIG_NAME_SUBJECT	= "subject";
	static final String		CONFIG_NAME_CONTENT	= "content";
	
	static final String		TYPE_SMS			= "sms";
	static final String		TYPE_CALL			= "call";
	static final String		TYPE_EMAIL			= "email";
	static final String		TYPE_BROWSER		= "browser";
	static final String		TYPE_HTML			= "html";
	static final String     TYPE_DOWNLOAD       = "download";
	static final String     TYPE_LOCALWAP       = "localwap";
	
	static final String		name				= "clk";
	String					target;
	String					subject;
	String					content;
	String browser_bg;
	String clkType="";
	int browser_jump;
	// String type = "Click";
	WeakReference<TVAdView>	weakContext;
	
//	 public Click(WeakReference<HdtMobileAdView>	weakContext) {
//			this.weakContext = weakContext;
//	 }
	
	static Click getInvoker(String localName, String qName, Attributes atts,WeakReference<TVAdView>	weakContext) throws SAXException
	{
		Click clk = null;
		if (localName.equals(Click.name) && atts != null)
		{
			int con = atts.getLength();
			for (int i = 0; i < con; i++)
			{
				String name = atts.getLocalName(i);
				if (name.equals(CONFIG_NAME_TYPE))
				{
					String type = atts.getValue(i);
//					clk.clkType=type;
					if (type.equals(TYPE_BROWSER))
					{
						clk = new ClkBrowser();
					}
					/*------ran.huang add 20121127 end------*/
					else
					{
						clk = getDefaultClickType();
					}
					clk.clkType=type;
				}
				if(name.equals(CONFIG_NAME_BG)){
					clk.browser_bg=atts.getValue(i);
				}
				if(name.equals(CONFIG_NAME_JUMP)){
					clk.browser_jump=Integer.parseInt(atts.getValue(i));
				}
				if (name.equals(CONFIG_NAME_TARGET))
				{
					if (clk == null)
					{
						clk = getDefaultClickType();
					}
					clk.target = atts.getValue(i);
				}
			}
		}
		if (clk == null)
		{
			clk = getDefaultClickType();
		}
		clk.weakContext = weakContext;
		return clk;
	}
	
	private static Click getDefaultClickType()
	{
		
		return new Click()
		{
			@Override
			void endElement(String localName, String qName, String content) throws SAXException
			{}
			
			@Override
			void startElement(String localName, String qName, Attributes atts) throws SAXException
			{}

			@Override
			boolean onClick(Context context, AdConfig config) {
				// TODO Auto-generated method stub
				return false;
			}
		};
	}
	
	/***********************************************************
	 * @author sun.huang MSN:jxhuanghua@yahoo.com.cn
	 * @version time��2011-4-19 ����07:13:32
	 * @param xmlPullParser
	 * @param name
	 * @throws XmlPullParserException
	 * @throws IOException
	 * @TODO �������XML
	 ***********************************************************/
	abstract void startElement(String localName, String qName, Attributes atts) throws SAXException;
	
	abstract void endElement(String localName, String qName, String content) throws SAXException;
	
	/***********************************************************
	 * @author sun.huang MSN:jxhuanghua@yahoo.com.cn
	 * @version time��2011-4-19 ����07:13:21
	 * @TODO �����������
	 ***********************************************************/
	abstract boolean onClick(Context context, AdConfig config);
}
